<template>
  <div>
    <el-form :model="formData" label-width="90px" :rules="rules" ref="addForm">
      <el-form-item label="岗位名称" prop="jobname">
        <el-input v-model="formData.jobname"></el-input>
      </el-form-item>
      <el-form-item label="岗位等级" prop="jobrank">
        <el-input v-model="formData.jobrank"></el-input>
      </el-form-item>
      <el-form-item label="岗位序列" prop="jobseries">
        <el-input v-model="formData.jobseries"></el-input>
      </el-form-item>
      <el-form-item label="公司部门" prop="pkCorp">
        <companyDepart @getCompanyDepart="getCompanyDepart" :companyDepartData="companyDepartData"></companyDepart>
      </el-form-item>
      <!-- <el-form-item label="所属公司" prop="pkCorp">
        <el-select v-model="formData.pkCorp" placeholder="请选择" @change="companyChange">
          <el-option
            v-for="item in companyList"
            :key="item.pkCorp"
            :label="item.unitname"
            :value="item.pkCorp"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门" prop="pkDeptdoc">
        <el-select v-model="formData.pkDeptdoc" placeholder="请选择">
          <el-option
            v-for="item in departList"
            :key="item.pkDeptdoc"
            :label="item.deptname"
            :value="item.pkDeptdoc"
          ></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="handSave()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  omJobUpdate,
  omJobAdd,
  omJobSelectBdCorp,
  omJobSelectBdDeptdoc
} from "../../../api/baseSetting";
import companyDepart from "../../components/companyDepart";
export default {
  components: {
    companyDepart
  },
  data() {
    return {
      companyDepartData: [], //传给公司部门的参数
      formData: {
        jobname: "",
        jobrank: "",
        jobseries: "",
        pkCorp: "",
        pkDeptdoc: "",
        groupId: "",
        groupName: "",
        corpTypeId: "",
        corpTypeName: ""
      },
      rules: {
        // name: [{ required: true, message: "请选择账号", trigger: "change" }],
        jobname: [
          { required: true, message: "请填写岗位名称", trigger: "change" }
        ],
        jobrank: [
          { required: true, message: "请填写岗位等级", trigger: "change" }
        ],
        jobseries: [
          { required: true, message: "请填写岗位序列", trigger: "change" }
        ],
        pkCorp: [{ required: true, message: "请选择公司部门", trigger: "blur" }]
        // pkDeptdoc: [
        //   { required: true, message: "请选择部门", trigger: "blur" }
        // ],
      },
      companyList: [], //公司列表
      departList: [] //部门列表
    };
  },
  props: {
    sendFormData: {
      num: {
        type: Object
      }
    }
  },
  methods: {
    getCompanyDepart(e) {
      this.formData.groupId = e[0].value;
      this.formData.groupName = e[0].label;

      this.formData.corpTypeId = e[1].value;
      this.formData.corpTypeName = e[1].label;

      this.formData.pkCorp = e[2].value;
      this.formData.pkDeptdoc = e[3].pkDeptdoc;
    },
    //点击保存
    handSave() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          var data = this.formData;
          if (this.sendFormData.pkOmJob) {
            //修改
            data.pkOmJob = this.sendFormData.pkOmJob;
            omJobUpdate(data).then(res => {
              this.$message({
                message: "成功",
                type: "success"
              });
              this.$emit("hideTotas");
            });
          } else {
            //新增
            omJobAdd(data).then(res => {
              this.$message({
                message: "成功",
                type: "success"
              });
              this.$emit("hideTotas");
            });
          }
        } else {
          this.$message({
            message: "请完善信息",
            type: "warning"
          });
        }
      });
    },
    //获取公司列表
    getCompany() {
      omJobSelectBdCorp({}).then(res => {
        this.companyList = res;
      });
    },
    //选择公司
    companyChange(pkCorp) {
      // console.log(pkCorp)
      this.formData.pkDeptdoc = "";
      this.getDepart(pkCorp);
    },
    //获取部门列表
    getDepart(pkCorp) {
      omJobSelectBdDeptdoc({ pkCorp }).then(res => {
        this.departList = res;
      });
    }
  },
  mounted() {
    // this.getCompany();
    // this.getDepart();
    let sendData = this.sendFormData;
    if (sendData.pkOmJob) {
      // this.getDepart(this.sendFormData.pkCorp);
      // this.formData.pkOmJob = this.sendFormData.pkOmJob;
      this.formData = sendData;
      this.companyDepartData = [
        sendData.groupId,
        sendData.corpTypeId,
        sendData.pkCorp,
        sendData.pkDeptdoc
      ];
    }
  }
};
</script>

<style lang="less" scoped>
</style>
